package com.applet.message.controller;


import com.applet.message.util.AliOSSUtil;
import com.applet.message.util.ResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/fileUpload")
@Api(value = "文件上传接口", description = "文件上传接口", tags = {"文件上传接口"})
public class FileUploadController {

    @Value("${pic.upload.dir}")
    private String picDir;

    @Value("${pic.upload.pre}")
    private String picPre;

    @Value("${pic.upload.endPoint}")
    private String endPoint;

    @Value("${pic.upload.bucketName}")
    private String bucketName;
    @Resource
    private AliOSSUtil aliOSSUtil;

    /**
     * 图片上传
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/uploadPic", method = RequestMethod.POST)
    @ApiOperation(value = "文件上传接口", httpMethod = "POST", response = ResultResponse.class, notes = "file")
    public ResultResponse upload(HttpServletRequest request) throws IOException {
        // 创建一个通用的多部分解析器
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        List<String> files = new ArrayList<>();
        boolean success = false;
        if (multipartResolver.isMultipart(request)) {
            // 转换成多部分request
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
            // 取得request中的所有文件名
            Iterator<String> iter = multiRequest.getFileNames();

            while (iter.hasNext()) {
                success = true;
                // 取得上传文件
                MultipartFile file = multiRequest.getFile(iter.next());
                if (file != null) {
                    String fileName = file.getOriginalFilename();
                    if (fileName != null && !"".equals(fileName.trim())) {
                        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
                        fileName = UUID.randomUUID().toString().replace("-", "") + "." + suffix;
                        File newFile = new File(fileName);
                        FileOutputStream outStream = new FileOutputStream(newFile);
                        outStream.write(file.getBytes());
                        outStream.close();
                        file.transferTo(newFile);
                        File compressNewFile=AliOSSUtil.compressImg(newFile);
                        // 上传到阿里云
                        Date date = new Date();
                        String dir = new SimpleDateFormat("yyyy/MM/dd").format(date);
                        files.add(picPre + aliOSSUtil.uploadFile(endPoint, bucketName, compressNewFile, picDir + "/istp/" + dir + "/"));
                        newFile.delete();
                        compressNewFile.delete();
                    }
                }
            }
        }
        if (success) {
            return new ResultResponse(success, files);
        } else {
            return new ResultResponse(success, "失败");
        }

    }
}
